﻿using AppFx.BizTalk.Diagnostics;
using log4net;
using Microsoft.BizTalk.Component.Interop;
using System;

namespace AppFx.BizTalk.PipelineComponents
{
    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
    [ComponentCategory(CategoryTypes.CATID_Any)]
    [System.Runtime.InteropServices.Guid(ComponentGuid)]
    public class LogMessage : BaseCustomPipelineComponent, IComponent, IComponentUI, IPersistPropertyBag
    {
        private static readonly ILog Log = LogFactory.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public const string ComponentGuid = "0267F045-3C3E-4F2C-9134-A9072B1F8074";
        public const string ComponentDescription = "Logs a custom message";
        public const string ComponentName = "LogMessage";


        public LogMessage() : base(ComponentName, typeof(LogMessage), ComponentDescription)
        {

        }

        public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(IPipelineContext pContext, Microsoft.BizTalk.Message.Interop.IBaseMessage pInMsg)
        {
            if (string.IsNullOrEmpty(_debugMessage) == false)
                Log.Debug(_debugMessage);

            if (string.IsNullOrEmpty(_infoMessage) == false)
                Log.Debug(_infoMessage);

            if (string.IsNullOrEmpty(_warnMessage) == false)
                Log.Debug(_warnMessage);
            
            return pInMsg;
        }

        public IntPtr Icon
        {
            get { return new IntPtr(); }
        }

        public System.Collections.IEnumerator Validate(object projectSystem)
        {
            return null;
        }

        #region Component Properties
        private string _debugMessage;
        private string _warnMessage;
        private string _infoMessage;

        public string InfoMessage
        {
            get { return _infoMessage; }
            set { _infoMessage = value; }
        }
        
        public string DebugMessage
        {
            get { return _debugMessage; }
            set { _debugMessage = value; }
        }
        
        public string WarnMessage
        {
            get { return _warnMessage; }
            set { _warnMessage = value; }
        }

        #endregion

        public void GetClassID(out Guid classID)
        {
            classID = new Guid(ComponentGuid);
        }

        public void InitNew()
        {
            //Do nothing
        }

        public void Load(IPropertyBag propertyBag, int errorLog)
        {
            try
            {
                base.ReadPropertyBag(propertyBag, "InfoMessage", ref _infoMessage);
                base.ReadPropertyBag(propertyBag, "DebugMessage", ref _debugMessage);
                base.ReadPropertyBag(propertyBag, "WarnMessage", ref _warnMessage);                
            }
            catch (Exception ex)
            {
                Log.Error("Error loading property bag", ex);                
                throw;
            }
        }

        public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
        {
            try
            {
                base.WritePropertyBag(propertyBag, "InfoMessage", _infoMessage);
                base.WritePropertyBag(propertyBag, "DebugMessage", _debugMessage);
                base.WritePropertyBag(propertyBag, "WarnMessage", _warnMessage);                
            }
            catch (Exception ex)
            {
                Log.Error("Error saving property bag", ex);                  
                throw;
            }
        }
    }
}

